package com.wandian.demo.controller
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	import flashx.textLayout.formats.TextAlign;

	import utils.UIHelper;

	/**
	 * 老虎机金额控制器
	 * @author long
	 */
	public class GameBetController extends Sprite
	{
		protected static var instance:GameBetController=null;
		private var boolShowMask:Boolean=false;
		private var startX:Number=0;
		private var startY:Number=439;
		public static const MSG_WELCOME:String=" WECOME TO THE BARBARY COAST";
		public static const MSG_BETUPTO:String=" BET UP TO 30 LINES FOR MAXIMUM WIN";
		public static const MSG_PRESSSPIN:String=" PRESS SPIN OR MAX SPIN TO START";
		public static const MSG_BETPERLINE:String=" BET 5 PER LINE";
		public static const MSG_SELECTLINES:String=" PLAYING {0} LINES ";
		public static const MSG_SELECTPERLINE:String=" PLAYING {0} LINES. {1} CREDIT PER LINE";
		private var _currentBet:int=0;
		/**
		 * 每线的最大押注
		 * @default
		 */
		public var maxBet:int=5;
		private var _currentBalance:Number=1000.0;
		private var _currentCredit:Number=10000.00; //10000.00;
		/**
		 * 比率数组 如balance=0.10*credit
		 * @default
		 */
		public var coinArray:Array=[0.02, 0.05, 0.10, 0.25, 0.50, 1.00];
		public var currentCoinIndex:int=2;

		/**
		 * spin之后的余额为-bet
		 * @default
		 */
		public function get currentCredit():Number
		{
			return _currentCredit;
		}

		/**
		 * @private
		 */
		public function set currentCredit(value:Number):void
		{
			_currentCredit=value;
		}

		/**
		 * spin减去余额
		 */
		public function subCreditAfterSpin():void
		{
			updateMoney();
			this.currentCredit=this.currentCredit - this.currentTotalBet;
		}

		/**
		 * spin减去余额
		 */
		public function checkMoney():Boolean
		{
			return !(this.currentTotalBet <= 0 || this.currentCredit <= 0);
		}

		public function canAddPerLine():Boolean
		{
			return (this.currentTotalBet < this.currentCredit) && (this.nextTotalBet <= this.currentCredit);
		}

		public function get currentBalance():Number
		{

			return (this.currentCredit - this.currentTotalBet) * Number(coinArray[this.currentCoinIndex]);
		}

		public function set currentBalance(value:Number):void
		{
			_currentBalance=value;
		}

		public var spinEnableFun:Function;

		public function GameBetController()
		{
			super();
			if (instance != null)
				throw new Error("Only one Singleton instance should be instantiated");
			instance=this;
			initConponent();
			this.x=this.startX;
			this.y=this.startY;
			initEventListener();
		}

		/**
		 * 当前每线押注
		 * @default
		 */
		public function get currentBet():int
		{
			return _currentBet;
		}

		/**
		 * @private
		 */
		public function set currentBet(value:int):void
		{
			if (this.currentTotalBet > this.currentCredit)
			{

				return;
			}
			this._tip_GameBetResult.text=this.currentTotalBet.toString();
			this._tip_GameBetResult.setTextFormat(this.fmt);
			_currentBet=value;
			updateMoney();
		}

		/**
		 * 更新当前余额显示
		 */
		private function updateMoney():void
		{
			_tip_GameCreditResult.text=(this.currentCredit - this.currentTotalBet).toString();
			_tip_GameBlanceResult.text=formatNumber(this.currentBalance, 2);
			this._tip_GameBlanceResult.setTextFormat(this.fmt);
			this._tip_GameCreditResult.setTextFormat(this.fmt);
		}

		/**
		 * 当前压线*每线bet
		 * @return
		 */
		public function get currentTotalBet():Number
		{
			return LinesController.Instance.getCurrentLinesCount() * this._currentBet;
		}

		/**
		 * 判断下次加柱的总额
		 * @return
		 */
		public function get nextTotalBet():Number
		{
			var nextbet:int=this._currentBet + 1;
			if (nextbet > this.maxBet)
			{
				this.currentBet=1;
			}
			return LinesController.Instance.getCurrentLinesCount() * nextbet;
		}

		public function initEventListener():void
		{
			this.addEventListener(LinesController.onSelectLinesChanged, onSelectLinesChangedHandler);
		}

		protected function onSelectLinesChangedHandler(event:Event):void
		{

			_tip_GamePlayResult.text=GameBetController.MSG_SELECTLINES.replace("{0}", LinesController.Instance.getCurrentLinesCount());
			this._tip_GamePlayResult.setTextFormat(this.fmt);
			if (this.canAddPerLine())
			{
				this.spinEnableFun("btn_betperline", true);
			}
		}

		/**
		 * 押注动作
		 */
		public function doBetPerLine():void
		{
			if (!this.canAddPerLine())
			{
				this.spinEnableFun("btn_betperline", false);
				return;
			}
			this._currentBet++;
			this.currentBet=this._currentBet;
			if (this.currentBet > this.maxBet)
			{
				this.currentBet=1;
			}
			if (this.currentTotalBet > 0)
			{
				this.spinEnableFun("btn_spin", true);
				this.spinEnableFun("btn_maxspin", true);
			}
			else
			{
				this.spinEnableFun("btn_spin", false);
				this.spinEnableFun("btn_maxspin", false);
			}
			_tip_GamePlayResult.text=GameBetController.MSG_SELECTPERLINE.replace("{0}", LinesController.Instance.getCurrentLinesCount()).replace("{1}", this.currentBet);
			this._tip_GamePlayResult.setTextFormat(this.fmt);
			if (!this.canAddPerLine())
			{
				this.spinEnableFun("", false);
			}
		}

		public function delEventListener():void
		{
			this.removeEventListener(LinesController.onSelectLinesChanged, onSelectLinesChangedHandler);
		}

		static public function get Instance():GameBetController
		{
			if (instance == null)
				instance=new GameBetController();
			return instance;
		}
		private var _textFormat:TextFormat;
		private var fmt:TextFormat;
		private var _tip_GamePlayResult:TextField=new TextField();
		private var _tip_GameBlanceResult:TextField=new TextField();
		private var _tip_GameCreditResult:TextField=new TextField();
		private var _tip_GameBetResult:TextField=new TextField();

		private function initConponent():void
		{
			this._textFormat=new TextFormat(null, 15, 0xffffff, true);
			fmt=new TextFormat();
			fmt.align=TextFormatAlign.CENTER;
			fmt.color=0x000000;
			fmt.size=16;
			_textFormat.align=TextAlign.CENTER;
			var filter:Array=null; //[new GlowFilter(0X778899, 0.5, 2, 2, 255)];
			UIHelper.addTextFieldChild(this, "Blance", null, 401, 3, false, this._textFormat, filter);
			_tip_GameBlanceResult=UIHelper.addTextFieldChild(this, this.currentBalance.toString(), _tip_GameBlanceResult, 401, 22, false, fmt, filter);
			UIHelper.addTextFieldChild(this, "Credit", null, 540, 3, false, this._textFormat, filter);
			_tip_GameCreditResult=UIHelper.addTextFieldChild(this, this.currentCredit.toString(), _tip_GameCreditResult, 540, 22, false, fmt, filter);
			UIHelper.addTextFieldChild(this, "Bet", null, 658, 3, false, this._textFormat, filter);
			_tip_GameBetResult=UIHelper.addTextFieldChild(this, "0", _tip_GameBetResult, 658, 22, false, fmt, filter);
			_tip_GamePlayResult=UIHelper.addTextFieldChild(this, "good luck", _tip_GamePlayResult, 135, 22, false, fmt, filter);
			_tip_GamePlayResult.autoSize=TextFieldAutoSize.CENTER;

			if (boolShowMask)
				UIHelper.addRectMaskSpriteChild(this);

		}

		/**
		 * 格式化小数点部分
		 * @param num
		 * @param nAfterDot
		 * @return
		 */
		private function formatNumber(num:Number, nAfterDot:int):String
		{
			var srcStr:String;
			var resultStr:String
			var dotPos:int;
			srcStr=(num).toString();
			srcStr="" + srcStr + "";
			var strLen:uint=srcStr.length;
			dotPos=srcStr.indexOf(".", 0);
			if (dotPos == -1)
			{
				resultStr=srcStr + ".";
				for (var i:uint=0; i < nAfterDot; i++)
				{
					resultStr=resultStr + "0";
				}
				return resultStr;
			}
			else
			{
				if ((strLen - dotPos - 1) >= nAfterDot)
				{
					var nAfter:int=dotPos + nAfterDot + 1;
					var nTen:int=1;
					for (var j:uint=0; j < nAfterDot; j++)
					{
						nTen=nTen * 10;
					}
					var resultnum:Number=Math.round(num * nTen) / nTen;
					resultStr=resultnum.toString();
					return resultStr;
				}
				else
				{
					resultStr=srcStr;
					for (i=0; i < (nAfterDot - strLen + dotPos + 1); i++)
					{
						resultStr=resultStr + "0";
					}
					return resultStr;
				}
			}
		}
	}
}
